//
//  SNSessionManager.m
//  app
//
//  Created by 王少宁 on 15/6/12.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNSessionManager.h"
#import "SNUniteUrlConstant.h"

@implementation SNSessionManager

+ (void)reloadSessionId
{
    SNBaseInfoManager * baseInfoManager = [SNBaseInfoManager shareManager];
    
    NSString * sessionId = [baseInfoManager baseInfoObjectForKey:@"cookiesSessionID"];
    
    if (sessionId && sessionId.length > 1) {
        [self saveCookiesWithSessionId:sessionId];
    }
}

+ (void)saveCookiesWithResponse:(JKResponse *)response
{
    NSHTTPCookie * cookie = [response getCookie:@"access_token"];
    
    NSString * sessionId = [cookie value];
    
    SNBaseInfoManager * baseInfoManager = [SNBaseInfoManager shareManager];
    
    if (sessionId && sessionId.length > 1) {
        [baseInfoManager setBaseInfoObject:sessionId forKey:@"cookiesSessionID"];
        [self saveCookiesWithSessionId:sessionId];
    }
    
}

+ (void)saveCookiesWithSessionId:(NSString *)sessionId
{
    if (!sessionId || sessionId.length < 1) {
        return;
    }
    
    NSArray * baseUrlArray = [NSArray arrayWithObjects:kBaseUrl,@"http://api.mylife114.com/", nil];
    
    for (NSString * subBaseUrl in baseUrlArray) {
        NSArray * urlArray = [subBaseUrl componentsSeparatedByString:@"/"];
        if (urlArray.count > 2) {
            NSString * cookieDomain = urlArray[urlArray.count-2];
            
            NSHTTPCookie * cookie = [SNSessionManager createCookieWithValue:sessionId domain:cookieDomain forName:@"access_token"];
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
            
        }
    }
}

+ (NSHTTPCookie *)createCookieWithValue:(NSString *)value domain:(NSString *)domain forName:(NSString *)name
{
    NSDictionary *properties = [[NSMutableDictionary alloc] init];
    [properties setValue:value forKey:NSHTTPCookieValue];
    [properties setValue:name forKey:NSHTTPCookieName];
    [properties setValue:domain forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60*24*30] forKey:NSHTTPCookieExpires];
    [properties setValue:@"/" forKey:NSHTTPCookiePath];
    return [[NSHTTPCookie alloc] initWithProperties:properties];
}

+ (void)clearCookies
{
    SNBaseInfoManager * baseInfoManager = [SNBaseInfoManager shareManager];
    
    NSArray * baseUrlArray = [NSArray arrayWithObjects:kBaseUrl,@"http://api.mylife114.com/", nil];
    
    [baseUrlArray enumerateObjectsUsingBlock:^(NSString * urlStr, NSUInteger idx, BOOL *stop) {
        NSMutableString * basUrl = [NSMutableString stringWithString:urlStr];
        [basUrl deleteCharactersInRange:NSMakeRange(urlStr.length-1, 1)];
        
        NSURL * url = [NSURL URLWithString:basUrl];
        if (url) {
            NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
            for (int i = 0; i < [cookies count]; i++) {
                NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
                
            }
        }
        
    }];
    
    NSString * sessionId = [baseInfoManager baseInfoObjectForKey:@"cookiesSessionID"];
    if (sessionId && sessionId.length > 1) {
        [baseInfoManager setBaseInfoObject:@"" forKey:@"cookiesSessionID"];
    }
    
}

@end
